<template>
  <div class="login">
    <div class="login-content">
      <h1 center>登录</h1>
      <el-form :model="ruleForm" :rules="rules" ref="formRef">
        <el-form-item label="账号" prop="userName">
          <el-input v-model="ruleForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="userpwd">
          <el-input v-model="ruleForm.userpwd"></el-input>
        </el-form-item>
        <el-button @click="login" type="primary">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useUser } from '../stores/modules/user.js'
import { useRouter } from 'vue-router/composables'

const user = useUser()
const router = useRouter()

const ruleForm = reactive({
  userName: 'admin',
  userpwd: ''
})
const rules = {
  userName: { required: true, message: '请输入账号', trigger: 'blur' },
  userpwd: { required: true, message: '请输入密码', trigger: 'blur' }
}
const formRef = ref(null)

const login = () => {
  formRef.value.validate(valid => {
    if (valid) {
      user.modifyUserName(ruleForm.userName)
      router.push('/home')
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
</script>

<style lang="scss" scoped>
.login {
  .login-content {
    margin: 0 auto;
    width: 500px;
    display: flex;
    flex-direction: column;
    text-align: center;
  }
}
</style>
